/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cjava.system.bean.config;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.cjava.system.model.Profile;
import org.cjava.system.service.interfac.IProfileService;
import org.cjava.system.util.Clazz;
import org.cjava.system.util.faces.FacesUtil;

/**
 *
 * @author user
 */
@ManagedBean
@SessionScoped
public class CVBean implements Serializable {

    private static final long serialVersionUID = 1L;
    ////////////////////////////////////////////
    @ManagedProperty("#{profileService}")
    IProfileService profileService;
    //////////////////////////////////////////////
    private Profile profile = new Profile();
    private String tipeProfileSelected = Clazz._TYPE_PROFILE._FRAMEWORK_JAVA;
    private List<String> listTipeProfile;
    private List<Profile> listProfile;

    public void initValuesForRegister() {
        //listTipeProfile = null;
        if (profile != null && profile.getType() != null) {
            tipeProfileSelected = profile.getType();
            profile = new Profile();
            profile.setType(tipeProfileSelected);
        } else {
            profile = new Profile();
        }
        listProfile = null;
    }

    public void saveOrUpdateCV() throws Exception {
        if (profile.getId() == null) {
            profileService.save(profile);
            FacesUtil.addMessage("Guardado con éxito", null, FacesMessage.SEVERITY_INFO);
            initValuesForRegister();
        } else {
            profileService.update(profile);
            FacesUtil.addMessage("Actualizado con éxito", null, FacesMessage.SEVERITY_INFO);
            initValuesForRegister();
        }
    }

    public void removeProfile(Profile prof) {
        try {
            profileService.HQLexecute("delete from Profile p where p.id=" + prof.getId());
            initValuesForRegister();
            FacesUtil.addMessage("Eliminado", null, FacesMessage.SEVERITY_INFO);
        } catch (Exception e) {
            FacesUtil.addMessage("Ya esta siendo usado, no se puede eliminar", null, FacesMessage.SEVERITY_ERROR);
        }

    }

    public void removeProfileInCascade(Profile prof) throws Exception {
        profileService.delete(prof);
        FacesUtil.addMessage("Todos los registros fueron eliminados", null, FacesMessage.SEVERITY_INFO);
    }

    public void filterPerType() throws Exception {
        String hql = "from Profile p where p.type='" + tipeProfileSelected + "'";
        listProfile = profileService.listHQL(hql);
    }

    public List<String> getListTipeProfile() {
        if (listTipeProfile == null) {
            listTipeProfile = new ArrayList<>();
            listTipeProfile.add(Clazz._TYPE_PROFILE._FRAMEWORK_JAVA);
            listTipeProfile.add(Clazz._TYPE_PROFILE._DATABASE);
            listTipeProfile.add(Clazz._TYPE_PROFILE._LANGUAGE);
            listTipeProfile.add(Clazz._TYPE_PROFILE._TOOLS);
            listTipeProfile.add(Clazz._TYPE_PROFILE._CERTIFICATION);
        }
        return listTipeProfile;
    }

    public Profile getProfile() {
        return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    public void setProfileService(IProfileService profileService) {
        this.profileService = profileService;
    }

    public String getTipeProfileSelected() {
        return tipeProfileSelected;
    }

    public void setTipeProfileSelected(String tipeProfileSelected) {
        this.tipeProfileSelected = tipeProfileSelected;
    }

    public List<Profile> getListProfile() throws Exception {
        if (listProfile == null) {
            String hql = "from Profile p where p.type='"
                    + tipeProfileSelected + "'";
            listProfile = profileService.listHQL(hql);
        }
        return listProfile;
    }

    public void setListProfile(List<Profile> listProfile) {
        this.listProfile = listProfile;
    }

}
